import java.util.ArrayList;

import controlP5.Textlabel;
import processing.core.PApplet;
import processing.core.PConstants;


public class GraphSet {
	private ArrayList<ArtistData> topForty;	//The 40 artists to be visualized
	private ArrayList<Integer> values;
	private int minVal, maxVal;
	
	private int x1, y1, x2, y2;	//Visualization boundries (corners)
	private int midLine;	//Midline y coordinate
	private int width, height, ellipseWidth;

	private ArrayList<Ellipse> ellipses;
	private ArrayList<Bar> bars;
	
	private boolean ellipseMode = true;

	public GraphSet(int _x1, int _y1, int _x2, int _y2) {
		x1 = _x1;
		y1 = _y1;
		x2 = _x2;
		y2 = _y2;

		topForty = new ArrayList<ArtistData>();
		values = new ArrayList<Integer>();
	}
	
	public void addArtist(ArtistData a) {
		topForty.add(a);
	}
	
	public void addVal(int v) {
		values.add(v);
	}
	
	public void replaceArtist(ArtistData oldArtist, ArtistData newArtist, int newValue) {
		int index = topForty.indexOf(oldArtist);
		values.remove(index);
		topForty.remove(oldArtist);
		
	}
	
	public boolean getMode() {
		return ellipseMode;
	}
	
	public void createGraphs() {
		minVal = values.get(values.size()-1);
		maxVal = values.get(0);
		
		midLine = (y1+y2)/2;
		width = x2-x1;
		height = y2-y1;
		ellipseWidth = width/values.size();
		
		createEllipses();
		createBars();
	}
	
	private void createEllipses() {
		ellipses = new ArrayList<Ellipse>();
		float x, h;
		
		int a1 = 20;
		int a2 = 21;
		
		for(int i=0; i<topForty.size(); i+=2) {
			x = PApplet.map(a1, 0, topForty.size(), x1, x2);
			h = PApplet.map(values.get(i), minVal, maxVal, 6, height*3);
			ellipses.add(new Ellipse(x, midLine, ellipseWidth+2, h, i, topForty.get(i).getName(), 
					topForty.get(i).getCount(), topForty.get(i).getValidUsersSize()));			
			
			if(i+1 >= topForty.size()) { break; }
			
			x = PApplet.map(a2, 0, topForty.size(), x1, x2);
			h = PApplet.map(values.get(i+1), minVal, maxVal, 6, height*3);
			ellipses.add(new Ellipse(x, midLine, ellipseWidth+2, h, i+1, topForty.get(i+1).getName(), 
					topForty.get(i+1).getCount(), topForty.get(i+1).getValidUsersSize()));
			
			a1--;
			a2++;
		}
	}
	
	private void createBars() {
		bars = new ArrayList<Bar>();
		float x, h;
		GLOBAL.startGradient();
		
		for(int i=0; i<topForty.size(); ++i) {
			x = PApplet.map(i, 0, topForty.size(), x1, x2); 
			h = PApplet.map(values.get(i), minVal, maxVal, 10, height*4);
			bars.add(new Bar(x, y2+50, ellipseWidth-3, h, GLOBAL.getNextGradientColor(), i, topForty.get(i).getName(), 
														topForty.get(i).getCount(), topForty.get(i).getValidUsersSize()));
		}
		
	}
	
	
	public void setMode(boolean mode) {
		ellipseMode = mode;
	}
	
	public void move(int _x1, int _y1, int _x2, int _y2) {
		x1 = _x1;
		y1 = _y1;
		x2 = _x2;
		y2 = _y2;
		
		midLine = (y1+y2)/2;
		width = x2-x1;
		height = y2-y1;
		ellipseWidth = width/values.size();

		
		GLOBAL.startGradient(); //Ensures that when graph is "moved," gradient is the same as before.
		createEllipses();
		createBars();
	}
	
	public void draw() {
		if(ellipseMode) {
			
			GLOBAL.processing.textAlign(PConstants.CENTER);
			GLOBAL.processing.textFont(GLOBAL.fontSegoe, 12);
			GLOBAL.processing.text("Artist Popularity\n(by total play count)", (x2+x1)/2, y2-50-height*2);
			GLOBAL.processing.textAlign(PConstants.LEFT);
			
			for(int x=0; x<ellipses.size(); ++x) {
				ellipses.get(x).draw();
			}
		}
		
		else {
			//Draw Axis
			GLOBAL.processing.stroke(0);
			GLOBAL.processing.line(x1-6, y2+height*2, x2, y2+height*2);
			GLOBAL.processing.line(x1-2, y2-50, x1-6, y2-50);
			GLOBAL.processing.line(x1-2, y2, x1-6, y2);
			
			GLOBAL.processing.textFont(GLOBAL.fontSegoe,9);
			GLOBAL.processing.textAlign(PConstants.RIGHT);
			GLOBAL.processing.text(""+maxVal, x1-6, y2-46);
			GLOBAL.processing.text(""+(int)((maxVal+minVal)/2), x1-6, y2+4);
			
			GLOBAL.processing.stroke(GLOBAL.colorMenu);
			GLOBAL.processing.line(x1, y2-50, x2, y2-50);
			GLOBAL.processing.line(x1, y2, x2, y2);
			
			GLOBAL.processing.textAlign(PConstants.CENTER);
			GLOBAL.processing.textFont(GLOBAL.fontSegoe, 12);
			GLOBAL.processing.text("Popularity\n(by total play count, descending)", (x2+x1)/2, y2-50-height*2);
			GLOBAL.processing.textAlign(PConstants.LEFT);
			
			for(int x=0; x<bars.size(); ++x) {
				bars.get(x).draw();
			}
		}
	}
	
}
